package com.pengcheng.taskapplication.net;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.pengcheng.taskapplication.mvp.view.activity.LoginActivity;
import com.pengcheng.taskapplication.mvp.view.activity.LoginActivityNew;

import rx.Subscriber;

/**
 * Created by kekex on 2019/2/28.
 * BaseSubscriber
 */

public abstract class BaseSubscriber<T> extends Subscriber<T> implements ProgressCancelListener{
    private ProgressDialogHandler mProgressDialogHandler;
    public Context context;
    private boolean isShowDialog=true;

    public BaseSubscriber(Context context) {
        this.context = context;
        mProgressDialogHandler = new ProgressDialogHandler(context, this, true);
    }


    public BaseSubscriber(Context context,boolean isShowDialog) {
        this.context = context;
        this.isShowDialog=isShowDialog;
        mProgressDialogHandler = new ProgressDialogHandler(context, this, true);
    }


    private void showProgressDialog(){
        if (mProgressDialogHandler != null&&isShowDialog) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }
    private void dismissProgressDialog(){
        if (mProgressDialogHandler != null&&isShowDialog) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {
        showProgressDialog();
    }
    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        dismissProgressDialog();
    }

    @Override
    public void onError(Throwable e) {
        try {
            dismissProgressDialog();
            String msg = "";
            if (e != null) {
                e.printStackTrace();
                msg = e.getMessage();
                Log.e("xuke", "onError msg: " + msg);
                if (msg.contains("timeout")
                        || msg.contains("java.net.ConnectException")
                        || msg.contains("java.net.SocketTimeoutException")
                        || msg.contains("failed")
                        || msg.contains("Failed to connect to")
                        || msg.contains("stream was reset")
                        || msg.contains("Unable to resolve host")
                        || msg.contains("SSL handshake time out")
                        || msg.contains("time out")) {
                    msg = "网络连接失败";
                    onFailed("1001", msg);



                    return;
                }

                if(msg.contains("401")){
//                    Intent intent =new Intent(context,LoginActivityNew.class);
//                    context.startActivity(intent);
                    onFailed("12","登录失败");
                    return;
                }




                if (msg.contains("java.lang.IllegalStateException")) {
                    Log.e("xuke", "java.lang.IllegalStateException");
                    return;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void onNext(T t) {
        Log.e("error",t.toString());
    }

    public abstract void onFailed(String code, String msg);

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }
}
